package com.show.service.sensor;

import com.show.dao.population.PopulationRepository;
import com.show.dto.sensor.GasSensor;
import com.show.dto.sensor.GasSensorError;
import com.show.util.QueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 有害气体传感器错误service
 *
 * @author 姜文
 */
@Service
public class GasSensorErrorService {

    private QueryUtil queryUtil;

    private PopulationRepository populationRepository;

    @Autowired
    public GasSensorErrorService(QueryUtil queryUtil, PopulationRepository populationRepository) {
        this.queryUtil = queryUtil;
        this.populationRepository = populationRepository;
    }


    /**
     * 有害气体传感器总数
     *
     * @author 姜文
     */
    public List<GasSensorError> gasSensorErrorTotal() {
        String sql = "SELECT * FROM `gas_sensor_error` GROUP BY gas_sensor_id";
        List<GasSensorError> gasSensorErrorList = queryUtil.query(sql, GasSensorError.class);
        return gasSensorErrorList;
    }

    /**
     * 有害气体传感器
     *
     * @author 姜文
     */
    public List<GasSensorError> gasSensorEvent() {
        String sql = "SELECT\n" +
                "\t*\n" +
                "FROM\n" +
                "\t`gas_sensor_error` gse\n" +
                "LEFT JOIN gas_sensor gs ON gse.gas_sensor_id = gs.gas_sensor_id\n" +
                "GROUP BY\n" +
                "\tgse.gas_sensor_id";
        List<GasSensorError> gasSensorErrorList = queryUtil.query(sql, GasSensorError.class);
        return gasSensorErrorList;
    }

    /**
     * 有毒气体传感器故障-大屏展示-点击事件
     *
     * @author 姜文
     */
    public List<GasSensorError> gasSensorError() {
        String sql = "SELECT\n" +
                "\tgse.gas_sensor_error_id AS GasSensorError,\n" +
                "cu.lng AS lon,\n" +
                "cu.lat AS lat\n" +
                "FROM\n" +
                "\t`gas_sensor_error` gse\n" +
                "LEFT JOIN gas_sensor gs ON gse.gas_sensor_id = gs.gas_sensor_id\n" +
                "LEFT JOIN cg_unit cu ON gs.unit_type_id = cu.unit_id";
        List<GasSensorError> gasSensorErrorList = queryUtil.query(sql, GasSensorError.class);
        return gasSensorErrorList;
    }

    /**
     * 有毒气体传感器故障事件-大屏展示-点击事件
     *
     * @author 姜文
     */
    public List<GasSensorError> gasSensorErrorEvent(String id) {
        String sql = "SELECT\n" +
                "\tgse.gas_sensor_error_id AS GasSensorError,\n" +
                "cu.lng AS lon,\n" +
                "cu.lat AS lat\n" +
                "FROM\n" +
                "\t`gas_sensor_error` gse\n" +
                "LEFT JOIN gas_sensor gs ON gse.gas_sensor_id = gs.gas_sensor_id\n" +
                "LEFT JOIN cg_unit cu ON gs.unit_type_id = cu.unit_id\n" +
                "WHERE gse.gas_sensor_error_id = '"+id+"'";
        List<GasSensorError> gasSensorErrorList = queryUtil.query(sql, GasSensorError.class);
        return gasSensorErrorList;
    }


    /**
     * 异常井室数量
     *
     * @return int 数量
     */
    public int queryGasErrorCount() {
        return gasSensorEvent().size();
    }


}
